package com.breezelin.freshmarket.adpaters;

/**
 * Created by Breeze Lin
 * 7:28 2016/5/30
 * 1linyufeng1@gmail.com
 */

import android.content.Context;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.breezelin.freshmarket.R;
import com.breezelin.freshmarket.entities.data.Product;
import com.squareup.picasso.Picasso;

import java.util.LinkedList;

/**
 * 相关商品适配器
 */
public class RelateAdapter extends Adapter {

	/**
	 * 上下文
	 */
	private Context context;
	/**
	 * 内容
	 */
	private LinkedList<Product> contents;
	/**
	 * 监听
	 */
	private RelateClickListener listener;

	public RelateAdapter(Context context, LinkedList<Product> contents, RelateClickListener listener) {
		this.context = context;
		this.contents = contents;
		this.listener = listener;
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		return new ProductHolder(LayoutInflater.from(context).inflate(R.layout.item_relate, parent,
				false));
	}

	@Override
	public void onBindViewHolder(ViewHolder holder, int position) {
		((ProductHolder) holder).setData(position);
	}

	private class ProductHolder extends ViewHolder implements View.OnClickListener {

		/**
		 * 当前条目中的数据
		 */
		private Product data;
		/**
		 * 封面
		 */
		private ImageView img;
		/**
		 * 产品名
		 */
		private TextView txt;

		public ProductHolder(View itemView) {
			super(itemView);
			img = (ImageView) itemView.findViewById(R.id.img);
			txt = (TextView) itemView.findViewById(R.id.txt);
			itemView.setOnClickListener(this);
		}

		/**
		 * 设置条目视图
		 *
		 * @param position 当前条目位置
		 */
		public void setData(int position) {
			data = contents.get(position);
			Picasso.with(context).load(data.getSmallPic()).into(img);
			txt.setText(data.getCommodityName());
		}

		@Override
		public void onClick(View v) {
			listener.onRelateClick(data);
		}
	}

	@Override
	public int getItemCount() {
		return contents.size();
	}

	public interface RelateClickListener {
		void onRelateClick(Product product);
	}
}
